import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DlgInscribir extends javax.swing.JDialog {
	private static final long serialVersionUID = 5270206369814097399L;
	
	private JLabel lblCedula;
	private JTextField txtCedula;
	private JTextField txtNombre;
	private JSeparator jSeparator1;
	private JButton btnCancelar;
	private JButton btnSalir;
	private JButton btnInscribir;
	private JTextField txtApellido;
	private JLabel lblApellido;
	private JLabel lblNombre;
	private Seccion seccion;
	
	/**
	* Auto-generated main method to display this JDialog
	*/
	/*public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				JFrame frame = new JFrame();
				DlgInscribir inst = new DlgInscribir(frame);
				inst.setVisible(true);
			}
		});
	}*/
	
	public DlgInscribir(JFrame frame, Seccion secc) {
		super(frame);
		this.setResizable(false);
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		seccion = secc;
		initGUI();
	}
	
	private void initGUI() {
		final DlgInscribir di = this;
		this.setModal(true);
		this.setTitle("Inscribir estudiante");
		try {
			{
				getContentPane().setLayout(null);
				this.setTitle("Inscribir Estudiante");
				{
					lblCedula = new JLabel();
					getContentPane().add(lblCedula);
					lblCedula.setText("Cedula:");
					lblCedula.setBounds(24, 18, 39, 13);
				}
				{
					txtCedula = new JTextField();
					getContentPane().add(txtCedula);
					txtCedula.setBounds(67, 15, 88, 20);
				}
				{
					lblNombre = new JLabel();
					getContentPane().add(lblNombre);
					lblNombre.setText("Nombre:");
					lblNombre.setBounds(24, 59, 41, 14);
				}
				{
					lblApellido = new JLabel();
					getContentPane().add(lblApellido);
					lblApellido.setText("Apellido:");
					lblApellido.setBounds(24, 100, 41, 14);
				}
				{
					txtNombre = new JTextField();
					getContentPane().add(txtNombre);
					txtNombre.setBounds(67, 56, 158, 21);
				}
				{
					txtApellido = new JTextField();
					getContentPane().add(txtApellido);
					txtApellido.setBounds(67, 97, 158, 21);
				}
				{
					btnInscribir = new JButton();
					getContentPane().add(btnInscribir);
					btnInscribir.setText("Inscribir");
					btnInscribir.setBounds(24, 177, 81, 20);
					btnInscribir.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
						  if(ValidarCampo())
						  {
							String ced = txtCedula.getText();
							
							if (seccion.buscarEstudiante(ced) != null){
								
								
								JOptionPane.showMessageDialog(null, "El Estudiante Ya Esta Inscrito","Informacion",JOptionPane.INFORMATION_MESSAGE);
								Reestablecer();
								return;
								
							}
							String nom = txtNombre.getText();
							String ape = txtApellido.getText();
							
							
							
							Estudiante e = new Estudiante(ced, nom,ape);
							
							seccion.inscribirEstudiante(e);
							JOptionPane.showMessageDialog(null, "Estudiante Inscrito","Informacion",JOptionPane.INFORMATION_MESSAGE);
                            Reestablecer();
						  }
						}
					} );
				}
				{
					btnSalir = new JButton();
					getContentPane().add(btnSalir);
					btnSalir.setText("Salir");
					btnSalir.setBounds(254, 176, 84, 21);
					btnSalir.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							di.dispose();
						}
					});
				}
				{
					btnCancelar = new JButton();
					getContentPane().add(btnCancelar);
					btnCancelar.setText("Reestablecer");
					btnCancelar.setBounds(126, 176, 88, 21);
					btnCancelar.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent evt) {
							Reestablecer();
						}
					});
				}
				{
					jSeparator1 = new JSeparator();
					getContentPane().add(jSeparator1);
					jSeparator1.setBounds(0, 147, 363, 10);
				}
			}
			this.setSize(371, 251);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public boolean ValidarCampo()
	{
		if(txtCedula.getText().equals(""))
		{
		   JOptionPane.showMessageDialog(null, "Ingrese una cedula","Campo Vacio",JOptionPane.INFORMATION_MESSAGE);
		   return false;
		}	else if(txtNombre.getText().equals(""))
		       {
				JOptionPane.showMessageDialog(null, "Ingrese un nombre","Campo Vacio",JOptionPane.INFORMATION_MESSAGE);	
				return false;
		       }
				else if(txtApellido.getText().equals("")){
			
					JOptionPane.showMessageDialog(null, "Ingrese un apellido","Campo Vacio",JOptionPane.INFORMATION_MESSAGE);
					return false;
				}
				else return true;
	}
	
	public void Reestablecer() {
		txtCedula.setText("");
		txtNombre.setText("");
		txtApellido.setText("");
	}

}
